/**
 * 
 */
package com.siteseat.common.i18n;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 */
public class Label {

  private String                     name;

  private MessageFormat              defaultFormat;

  private Map<String, MessageFormat> others;

  /**
   * @param name
   * @return
   */
  public static Label createEmergentLabel( String name ) {
    Label result = new Label();
    result.setName( name );
    result.setDefault( name );
    result.setOthers( new HashMap<String, String>( 0 ) );
    return result;
  }

  /**
   * @return the name
   */
  public String getName() {
    return name;
  }

  /**
   * @param name
   *          the name to set
   */
  public void setName( String name ) {
    this.name = name;
  }

  /**
   * @param language
   * @return
   */
  public MessageFormat resolveFormat( String language ) {
    MessageFormat result = getOthers().get( language );
    return ( result == null ) ? this.defaultFormat : result;
  }

  /**
   * @param language
   * @return
   */
  public MessageFormat resolveFormat( Locale locale ) {
    return resolveFormat( locale.toString() );
  }

  /**
   * @param language
   * @param args
   * @return
   */
  public String formatMessage( String language, Object[] args ) {
    MessageFormat fmt = resolveFormat( language );
    return fmt.format( args );
  }

  /**
   * @param language
   * @param args
   * @return
   */
  public String formatMessage( Locale locale, Object[] args ) {
    MessageFormat fmt = resolveFormat( locale );
    return fmt.format( args );
  }

  /**
   * @param language
   * @param args
   * @return
   */
  public String formatDefaultMessage( Object[] args ) {
    return defaultFormat.format( args );
  }

  /**
   * @return the defaultValue
   */
  public MessageFormat getDefault() {
    return defaultFormat;
  }

  /**
   * @param defaultValue
   *          the defaultValue to set
   */
  public void setDefault( String defaultValue ) {
    this.defaultFormat = new MessageFormat( defaultValue );
  }

  /**
   * @return the other
   */
  public Map<String, MessageFormat> getOthers() {
    return others;
  }

  /**
   * @param other
   *          the other to set
   */
  public void setOthers( Map<String, String> other ) {
    this.others = new HashMap<String, MessageFormat>();
    for( Map.Entry<String, String> entry: other.entrySet() ) {
      this.others.put( entry.getKey(), new MessageFormat( entry.getValue() ) );
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public String toString() {
    return getName();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public boolean equals( Object obj ) {
    if( ( obj instanceof Label ) == false ) {
      return false;
    }
    return getName().equals( ( (Label)obj ).getName() );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public int hashCode() {
    return getName().hashCode();
  }

}
